package logic;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import data.Connect;
import ui.AdminPanel;
import ui.AdminUI;
import ui.GlobalLogin;
import ui.GlobalLogoutPanel;
import ui.GlobalScreen;
import ui.UserUI;

public class ACListLogin implements ActionListener {
	GlobalScreen screen;
	GlobalLogin login;
	Connect connect;
	AdminUI admin;
	GlobalLogoutPanel logoutpanel;
	UserUI userui;
	AdminPanel adminpanel;
	String PassString;

	public ACListLogin(GlobalScreen screen, GlobalLogin login, Connect connect,
			AdminUI admin, GlobalLogoutPanel logoutpanel, UserUI userui) {

		this.screen = screen;
		this.login = login;
		this.connect = connect;
		this.admin = admin;
		this.logoutpanel = logoutpanel;
		this.userui = userui;
		this.adminpanel = adminpanel;

	}

	@Override
	public void actionPerformed(ActionEvent e) {

		// Creating Variables which we use in our actions
		String ac = e.getActionCommand();

		connect = new Connect();
		// Actions Performed
		if (ac.equals("buttonLogin")) {
			String username = login.getUserfield().getText();
			char[] PassChar = login.getPassfield().getPassword();
			PassString = String.valueOf(PassChar);

			if (connect.authenticateUser(username, PassString)) {
				userui.getAc().setCurrentUser(connect.getUser(username));
				if (connect.isAdmin) {
					screen.show("adminui");
					admin.addLogoutPanel();
					login.notification.setVisible(false);

				} else {
					screen.show("userui");
					screen.userui.addLogoutPanel();
					login.notification.setVisible(false);
				}
			} else {
				login.notification.setVisible(true);
			}
		}

		if (ac.equals("buttonLogout")) {
			connect.isAuthenticated = false;
			connect.isAdmin = false;
			screen.show("globallogin");
			login.getUserfield().setText("");
			login.getPassfield().setText("");

		}

	}
}
